VERSION --use-cache-command --explicit-global --shell-out-anywhere --use-registry-for-with-docker  --parallel-load 0.6
ARG --global BASE_IMAGE_VERSION=alpine
ARG --global NODE_IMAGE_VERSION=16.18.1
ARG --global NGINX_IMAGE_VERSION=1.23.1-alpine
ARG --global PYTHON_IMAGE_VERSION=3.9.13-bullseye
FROM earthly/dind:${BASE_IMAGE_VERSION}
WORKDIR /app
COPY .version .
ARG --global PIPELINE_ID
ARG --global APP_BRANCH_NAME
ARG --global DOCKER_LOGIN_PASSWORD
ARG --global DOCKER_LOGIN_USER=pipinet@vip.qq.com
ARG --global APP_BASE_VERSION=$(cat .version)
ARG --global DOCKER_REGISTRY=registry.cn-hangzhou.aliyuncs.com
ARG --global DOCKER_IMAGE_NAME=handao/qar-explorer-web

IF [ -z "${PIPELINE_ID}"  ]
  ARG --global APP_VERSION=${APP_BASE_VERSION}.local
ELSE
  ARG --global APP_VERSION=${APP_BASE_VERSION}.${PIPELINE_ID}-${APP_BRANCH_NAME}
END

build-base:
  FROM node:${NODE_IMAGE_VERSION}
  WORKDIR /app
  COPY ./docker/repositories /etc/apk/
  COPY package.json \
       .yarnrc \
       yarn.lock \
       ./
  RUN  yarn install --frozen-lockfile
  SAVE ARTIFACT node_modules

check:
  FROM +build-base
  COPY . .
  RUN yarn run prepush

package:
  FROM +build-base
  COPY . .
  RUN echo "VITE_APP_VERSION=${APP_VERSION}" > .env
  RUN yarn run build-prod
  SAVE ARTIFACT dist

build-image-base:
  FROM nginx:${NGINX_IMAGE_VERSION}
  ENV RUNTIME_CONTEXT_PATH=""
  ENV RUNTIME_PUBLIC_URL=""

  COPY ./docker/repositories /etc/apk/

  RUN set -ex; \
       apk update --no-cache; \
       apk add --no-cache bash wget;
  COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
  COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf
  RUN apk add --no-cache bash curl gawk sed grep bc coreutils
  COPY docker/docker-entrypoint.d/89-setup-app-config-suffix.sh \
       docker/docker-entrypoint.d/90-setup-runtime-public-url.sh \
       docker/docker-entrypoint.d/91-setup-nginx.sh \
       /docker-entrypoint.d/

build-image:
  FROM +build-image-base
  ENV APP_VERSION=${APP_VERSION}
  ENV RUNTIME_CONTEXT_PATH=''
  ENV RUNTIME_PUBLIC_URL=''
  COPY +package/dist /app
  VOLUME /var/log/nginx
  EXPOSE 80
  CMD ["nginx", "-g", "daemon off;"]
  SAVE IMAGE --push ${DOCKER_REGISTRY}/${DOCKER_IMAGE_NAME}:${APP_VERSION}

deploy-base:
  FROM python:${PYTHON_IMAGE_VERSION}
  WORKDIR /app
  COPY ./docker/sources.list /etc/apt/
  COPY ./.deploy .
  RUN mkdir -m 600 ~/.ssh \
      && mv id_rsa.pub ~/.ssh/ \
      && mv id_rsa ~/.ssh/ \
      && chmod 600 ~/.ssh/id_rsa.pub \
      && chmod 600 ~/.ssh/id_rsa
  RUN pip install -r ./requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
  SAVE IMAGE --cache-hint

deploy:
  FROM +deploy-base
  ARG INVENTORY
  RUN ansible-playbook \
       -i ./inventories/"${INVENTORY}"/host.ini \
       -e "version=${APP_VERSION} app_name=qar-explorer-web-${INVENTORY} DOCKER_REGISTRY=${DOCKER_REGISTRY} DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME} DOCKER_LOGIN_USER=${DOCKER_LOGIN_USER} DOCKER_LOGIN_PASSWORD=${DOCKER_LOGIN_PASSWORD}" \
       ./qar-explorer-web.yaml

ci-check:
  BUILD +check

ci-package:
  BUILD +package
  BUILD +build-image

ci-deploy:
  ARG INVENTORY
  BUILD +deploy --INVENTORY=${INVENTORY}
